package com.feihao.zhou.DruidPoolTest;

import com.alibaba.druid.pool.DruidDataSource;
import com.alibaba.druid.pool.DruidDataSourceFactory;

import javax.sql.DataSource;
import java.io.IOException;
import java.io.InputStream;
import java.sql.Connection;
import java.sql.Date;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.util.Properties;

public class TestDruidPool {
    public static void main(String[] args) throws Exception {

        //读取配置文件：
        InputStream is = TestDruidPool.class.getClassLoader().getResourceAsStream("druid.properties");
        Properties pro = new Properties();
        pro.load(is);

        //1.获取池对象：
        DataSource dataSource = DruidDataSourceFactory.createDataSource(pro);

        //获取连接：
        Connection connection = dataSource.getConnection();

        //获取执行对象：
        //sql：
        String sql = "SELECT * FROM student WHERE sid = '4'";
        PreparedStatement preparedStatement = connection.prepareStatement(sql);

        //执行：
        ResultSet resultSet = preparedStatement.executeQuery();

        while (resultSet.next()) {
            Integer sid = resultSet.getInt("sid");
            String name = resultSet.getString("name");
            Integer age = resultSet.getInt("age");
            Date birthday = resultSet.getDate("birthday");
            System.out.println(sid + " " + name + " " + age + " " + birthday);
        }

        //释放资源：
        resultSet.close();
        connection.close();
        preparedStatement.close();
    }
}
